//
//  ELRadarChart Cell.m
//  jobClient
//
//  Created by zoulf622 on 2023/11/24.
//  Copyright © 2023 YL1001. All rights reserved.
//

#import "ELRadarChartCell.h"

@interface ELRadarChartCell()<WKUIDelegate,WKNavigationDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *top_const;
@property (weak, nonatomic) IBOutlet UIView *backView;

@property (weak, nonatomic) IBOutlet UIView *gradientView;

@property (weak, nonatomic) IBOutlet UILabel *title1_lab;

@property (weak, nonatomic) IBOutlet UIView *radChartView;

@property (weak, nonatomic) IBOutlet UILabel *maxValue_lab;

@property (weak, nonatomic) IBOutlet UILabel *maxContent_lab;

@property (weak, nonatomic) IBOutlet UILabel *minValue_lab;

@property (weak, nonatomic) IBOutlet UILabel *minContent_lab;

@property (weak, nonatomic) IBOutlet UIView *maxGradView;

@property (weak, nonatomic) IBOutlet UIView *minGradView;

@property (weak, nonatomic) IBOutlet UIView *maxRoundView;

@property (weak, nonatomic) IBOutlet UIView *minRoundView;

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation ELRadarChartCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    _top_const.constant = (0.6 * ScreenWidth * 66/239.0) * 0.92;
    
    [_maxRoundView rounded:2];
    [_maxRoundView setELShadow:UIColorFromRGB(0xCAD8BC) opacity:1 radius:2.0 offset:CGSizeMake(0, 2)];
    
    [_minRoundView rounded:2];
    [_minRoundView setELShadow:UIColorFromRGB(0xCAD8BC) opacity:1 radius:2.0 offset:CGSizeMake(0, 2)];
    
    
    [self.radChartView addSubview:self.webView];
    
    
    // Initialization code
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [_gradientView gradientWithStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1) Colors:@[(__bridge id)UIColorFromRGB(0xBFF1B4).CGColor,(__bridge id)UIColorFromRGB(0xFEFDEB).CGColor]];
    [_gradientView round:8 RectCorners:UIRectCornerTopLeft];
    [_backView round:8 RectCorners:UIRectCornerTopLeft];
    
    [_maxGradView gradientWithStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0) Colors:@[(__bridge id)UIColorFromRGB(0x88C881).CGColor,(__bridge id)UIColorFromRGB(0xFEFDEB).CGColor]];
    [_minGradView gradientWithStartPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0) Colors:@[(__bridge id)UIColorFromRGB(0x88C881).CGColor,(__bridge id)UIColorFromRGB(0xFEFDEB).CGColor]];
}

-(void)setModel:(ELCareerMeasureResultModel *)model{
    
    _model = model;
    //标题
    NSString *name_title = [NSString stringWithFormat:@"根据不同的生活环境以及个人成长过程的不同抉择，每个人会逐渐体现出独特的属性，%@察觉到你的六维属性如下：",_model.protectorName];
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc]init];
    paraStyle.lineSpacing = 8;
    NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:15 weight:UIFontWeightMedium],NSParagraphStyleAttributeName:paraStyle};
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:name_title
                                                                                       attributes:attribute];
    NSRange range = [name_title rangeOfString:_model.protectorName];
    [attributeString addAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0xED5C22)} range:range];
    _title1_lab.attributedText = attributeString;
    _title1_lab.numberOfLines = 0;
    
    //最高描述
    _maxValue_lab.text = _model.maxScoreDimension.propertyName;
    
    NSMutableParagraphStyle *maxParaStyle = [[NSMutableParagraphStyle alloc]init];
    maxParaStyle.lineSpacing = 8;
    NSDictionary *maxAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular],NSParagraphStyleAttributeName:maxParaStyle};
    
    NSMutableAttributedString *maxAttributeString = [[NSMutableAttributedString alloc]initWithString:_model.maxScoreDimension.propertyDesc
                                                                                       attributes:maxAttribute];
    _maxContent_lab.attributedText = maxAttributeString;
    
    //最低
    _minValue_lab.text = _model.minScoreDimension.propertyName;
    
    NSMutableParagraphStyle *minParaStyle = [[NSMutableParagraphStyle alloc]init];
    minParaStyle.lineSpacing = 8;
    NSDictionary *minAttribute = @{NSFontAttributeName:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular],NSParagraphStyleAttributeName:minParaStyle};
    
    NSMutableAttributedString *minAttributeString = [[NSMutableAttributedString alloc]initWithString:_model.minScoreDimension.propertyDesc
                                                                                       attributes:minAttribute];
    _minContent_lab.attributedText = minAttributeString;
    
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"radChart.html" ofType:nil];
    NSString *htmlStr = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    [_webView loadHTMLString:htmlStr baseURL:[[NSBundle mainBundle] bundleURL]];
    
    
}
    

- (WKWebView *)webView{
    if (_webView == nil) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0,0,(ScreenWidth - 32) *0.9,(ScreenWidth - 32) *0.9) configuration:config];
        _webView.scrollView.scrollEnabled = NO;
        _webView.backgroundColor = UIColor.clearColor;
        _webView.allowsBackForwardNavigationGestures = NO;
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        
    }
    return _webView;
}

//开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"didStartProvisionalNavigation");
}

//正在加载
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    NSLog(@"didCommitNavigation");
}

//加载完成,可以相互调用了
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"didFinishNavigation");
    
    

    
    NSArray *radCharts = _model.radarChart;
    //先找到最大值
    NSString *maxValue = @"0";
    for (ELRadarChartModel *chartModel in radCharts) {
        if (maxValue.floatValue < chartModel.propertyScore.floatValue) {
            maxValue = chartModel.propertyScore;
        }
    }
    
    NSMutableArray *seriesData = [NSMutableArray array];
    NSMutableArray *indicatorData = [NSMutableArray array];
    
    for (ELRadarChartModel *chartModel in radCharts) {
        [seriesData addObject:chartModel.propertyScore];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:chartModel.propertyName forKey:@"description"];
        [dict setValue:maxValue forKey:@"max"];
        [dict setValue:chartModel.propertyScore forKey:@"value"];
        [indicatorData addObject:dict];
    }
    
    
    
    NSString *javascript = [NSString stringWithFormat:@"setData(%@, %@);", [self convertToJsonString:indicatorData], [self convertToJsonString:seriesData]];
    [self.webView evaluateJavaScript:javascript completionHandler:^(id _Nullable name, NSError * _Nullable error) {
       NSLog(@"方法调用完成回调");
    }];
    
}
- (NSString *)convertToJsonString:(NSArray *)array {
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:0 error:&error];
    if (!jsonData) {
        return @"[]";
    } else {
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
